import $ from 'jquery'
import './css/test.css'
import Vue from '../node_modules/vue/dist/vue.js'

import VueRouter from 'vue-router'
Vue.use(VueRouter)

import Vuex from 'vuex' //注册vuex
Vue.use(Vuex)

//每次刚进入网站，肯定会调用main.js在刚调用的时候，先从本地存储中，把购物车的数据读取出来，放到store中
 var car = JSON.parse(localStorage.getItem('car') || '[]')

var store = new Vuex.Store({
    state:{ //this.$store.state.***
        car:car //将购物车中的商品的数据，用一个数组存储起来，在car数组中存储一些商品的对象。{id,count,price,selsected}
    },
    mutations:{ //this.$store.commit('方法名称'，'参数')
        addToCar(state,goodsinfo){
            var flag = false; //假设在购物车中没有找到对应的商品
            state.car.some(item=>{ //先在购物车中找有没有对应的商品，有就更新数量
                if(item.id == goodsinfo.id){
                    item.count += parseInt(goodsinfo.count);
                    flag = true;
                    return true;
                }
            })
            if(flag == false){   //没有就push到car中
                state.car.push(goodsinfo)
            };
           
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        updateGoodsInfo(state,goodsinfo){
            state.car.some(item=>{
                if(item.id == goodsinfo.id){
                    item.count = parseInt(goodsinfo.count)
                    return true
                }
            });
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        removeFromCar(state,id){
            //根据id，从store中的购物车中删除对应的数据
            state.car.some((item,i)=>{
                if(item.id == id){
                   
                    state.car.splice(i,1)
                    return true;
                }
            });
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateGoodsSelected(state,info){
            state.car.some(item=>{
                if(item.id == info.id){
                    item.selected = info.selected
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        }

    },
    getters:{ //this.$store.getters.***
        getAllCount(state){
            var c = 0;
            state.car.forEach(item=>{
                c += item.count
            })
            return c
        },
        getGoodsCount(state){
            var o ={}
            state.car.forEach(item =>{
                o[item.id] = item.count
            })
            return o;
        },
        getGoodsSelected(state){
            var o= {}
            state.car.forEach(item =>{
                o[item.id] = item.selected
            })
            return o
        },
        getGoodsCountAndAmount(state){
            var o = {
                count:0,
                amount:0
            }
            state.car.forEach(item=>{
                if(item.selected){
                    o.count += item.count
                    o.amount += item.price * item.count
                }
            })
            return o
        }
    }
})
import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.root='http://localhost:3000/' //通过全局配置了请求的数据接口,可以修改接口地址在局域网上用
Vue.http.options.emulateJSON = true;   //全局设置post时候表单数据格式组织形式
import moment from 'moment'
Vue.filter('dateFormat',function(dateStr,pattern = "YYYY-MM-DD HH:mm:ss"){
    return moment(dateStr).format(pattern)
})

import 'bootstrap/dist/css/bootstrap.css'
import './lib/mui/css/mui.css'
import './lib/mui/css/icons-extra.css'

 import MintUI from 'mint-ui'
 Vue.use(MintUI)
 import 'mint-ui/lib/style.css'         //使用懒加载需要全部导入mint-ui

import VuePreview from 'vue-preview'     //安装导入图片预览插件
Vue.use(VuePreview)

/* import {Button,Header,Swipe,SwipeItem,Lazyload} from 'mint-ui'
// Vue.component(Toast.name,Toast)
Vue.component(Button.name,Button)
Vue.component(Header.name,Header)
Vue.component(Swipe.name,Swipe)
Vue.component(SwipeItem.name,SwipeItem)
Vue.use(Lazyload); */


import App from './App.vue'

import router from './router.js' //导入路由模块

var vm = new Vue({
    el:"#app",
    data:{
        msg:123
    },
    render:c => c(App),
    router,
    store
})